Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check other potential alises if Docker Hub #605

Merged
merged 10 commits into from
Jul 17, 2018
Merged

Conversation

chanseokoh
Copy link
Member

@chanseokoh chanseokoh commented Jul 13, 2018

Part of #586.

@chanseokoh chanseokoh requested a review from a team July 13, 2018 16:21
Copy link
Contributor

@coollog coollog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @chanseokoh ! This looks like it works for inferring credential helpers, but #586 would involve adding the alias for the Docker config retrieval (DockerConfigCredentialRetriever)

}

@VisibleForTesting
Authorization findOtherDockerHubCredential(String dockerHubRegistry)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we want to try to keep the build steps (AsyncStep) as minimal as possible and delegate the real logic to other classes. So, here for example, we should have probably put the credential helper inference logic inside a DockerCredentialHelperInferer rather than in this AsyncStep so that we could unit test against it well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to verify, what I understand from your comments is to minimizing code lines in AsyncStep classes by moving code out to different classes, and not about minimizing operations and time the steps take, right?

@coollog coollog added this to the v0.9.7 milestone Jul 13, 2018
@chanseokoh
Copy link
Member Author

Looking at the code of RetrieveRegistryCredentialsStep again, looks like it's enough to look inside ~/.docker/config for finding other potential Docker Hub aliases (i.e., no need to change RetrieveRegistryCredentialsStep but only change DockerConfigCredentialRetriever).

I initially thought it would be more complete to do this at the RetrieveRegistryCredentialsStep level so that it would also try other credential supplier, but looks like knownRegistryCredentials is independent of the given registry name and the credential helpers are based only on the given suffix and the commonly known suffixes. @coollog am I getting this right?

@chanseokoh
Copy link
Member Author

chanseokoh commented Jul 16, 2018

Ready for review.

I've talked to @coollog, and for now as the first step and a quick fix, this PR just looks into docker.config. However, eventually we should also look for credentials with registry aliases in other sources than docker.config (for example, settings.xml).

@chanseokoh
Copy link
Member Author

Ready for review, but honestly

Oops. Was going to say honestly, I am not sure if there should be some special handling for the extra path /v1/ as in index.docker.io/v1/.

* @param registry the registry for which the alias group is requested
* @return non-empty list of registries where {@code registry} is the first element
*/
public static ImmutableList<String> getAliasesGroup(String registry) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should return List to minimize exposing Guava classes in APIs. Right?

Copy link
Member Author

@chanseokoh chanseokoh Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I didn't intend for this class to be part of the public API, but I guess there is no way to keep it private to the API while the class is declared public. I'll change it to List.

@chanseokoh
Copy link
Member Author

@coollog said the way it manipulated the list to place the given registry at the front (i.e., removing the element and adding it at the front in sequence) did not really feel natural. I agree, so I updated to do it in the functional way.

Copy link
Contributor

@coollog coollog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just some small comments.

import com.google.common.collect.ImmutableList;
import java.util.ArrayDeque;

public class RegistryAliasGroup {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistent style, we include a javadoc for each class.

private static final ImmutableList<ImmutableList<String>> REGISTRY_ALIAS_GROUPS =
ImmutableList.of(
// Docker Hub alias group
ImmutableList.of("registry.hub.docker.com", "index.docker.io"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no need to be a list.

// Found a group. Move the requested "registry" to the front before returning it.
Stream<String> self = Stream.of(registry);
Stream<String> withoutSelf = aliasGroup.stream().filter(alias -> !registry.equals(alias));
return Stream.concat(self, withoutSelf).collect(Collectors.toList());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could even return the stream itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried returning a stream, but it does not seem concise at the caller side. We can switch to Stream later if it seems fit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. For later, the caller could use something like:

Authorization authorization = getAliasesGroup()
    .map(registryAlias -> retrieve(dockerConfigTemplate, registryAlias))
    .filter(Objects::nonNull)
    .findFirst();
if (authorization.isPresent()) {
  return authorization.get();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably even do .findFirst().orElse(null);


@Test
public void testGetAliasesGroup_registryHubDockerCom() {
Assert.assertArrayEquals(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think in this codebase, we prefer to use List over raw arrays, such that this can be like Assert.assertEquals(theList, Arrays.asList(a, b, c).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is certainly better.

@@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

- `jib:buildTar` goal to build an image tarball at `target/jib-image.tar`, which can be loaded into docker using `docker load` ([#514](https://github.com/GoogleContainerTools/jib/issues/514))

- For Docker Hub, also tries registry aliases when getting a credential from `docker.config`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: probably just call it "the Docker config" - docker.config may imply something else
Also, for formatting, it's good to include the pull request/issue number/link and I think this line should probably be under Fixed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, also adding this to jib-gradle-plugin.

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RegistryAliasGroup {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistent style, we include a javadoc for each class

@@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

- `jibBuildTar` task to build an image tarball at `build/jib-image.tar`, which can be loaded into docker using `docker load` ([#514](https://github.com/GoogleContainerTools/jib/issues/514))

- For Docker Hub, also tries registry aliases when getting a credential from the Docker config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit: for consistency, we should keep the entries with no empty lines inbetween (line 8 should not actually be there either)

@chanseokoh chanseokoh merged commit f3a725b into master Jul 17, 2018
@chanseokoh chanseokoh deleted the i586-docker-aliases branch July 17, 2018 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants